"use strict";
// // 引入test模块
// var greet = require("./test.js");

// var s = "michael";

// greet.greet(s);

// console.log(greet.s);

var fs = require("fs");

function test1() {
  fs.readFile("./node-test/test.js", "utf-8", function (err, data) {
    if (err) {
      console.log(err);
    } else {
      console.log(data);
    }
  });
}

function test2() {
  var data = "hello,test";
  fs.writeFile("output.txt", data, function (err) {
    if (err) {
      console.log(err);
    } else {
      console.log("ok");
    }
  });
}

function test3() {
  fs.stat("output.txt", function (err, stat) {
    if (err) {
      console.log(err);
    } else {
      console.log("isFile" + stat.isFile());
      console.log("isDir" + stat.isDirectory());
    }
    if (stat.isFile) {
      console.log("size:" + stat.size);
      console.log("birth time" + stat.birthtime);
      console.log("modified time" + stat.mtime);
    }
  });
}

function test4() {
  var data = fs.statSync("output.txt");
  console.log(data);
}

function test5() {
  var rs = fs.createReadStream("output.txt", "utf-8");
  rs.on("data", function (chunk) {
    console.log("data:", chunk);
  });
  rs.on("end", function () {
    console.log("end");
  });
  rs.on("error", function (err) {
    console.log("error:", err);
  });
}

/**
 * 测试写入
 */
function test6() {
  var ws = fs.createWriteStream("output.txt", "utf-8");
  ws.write("使用stream写入文本数据。。。。。。。\n");
  ws.write("end");
  ws.end;
  var ws2 = fs.createWriteStream("output2.txt");
  ws2.write(new Buffer("使用stream写入二进制数据。。。。。\n", "utf-8"));
  ws2.end;
}

/**
 * 测试pipe
 */
function test7() {
  var rs = fs.createReadStream("output.txt");
  var ws = fs.createWriteStream("output2.txt");
  rs.pipe(ws);
}

var http = require("http");
function test8() {
  var server = http.createServer(function (req, resp) {
    console.log(req.method + ":" + req.url);
    resp.writeHead(200, { "Content-Type": "text/html" });
    resp.end("<h1>Hello world!</h1>");
  });
  server.listen(8080);
  console.log("监听8080端口");
}

const crypto = require("crypto");
const hash = crypto.createHash("md5");
function test9() {
  hash.update("hello,world");
  hash.update("hello,node");
  console.log(hash.digest("hex"));
}

function test10() {
  fs.readFile("output.txt", function (error, data) {
    console.log(error, data);
    console.log(data.toString());
  });
}
test10();
